import { createRouter, createWebHistory } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';
import { useUserStore } from '@/stores/user'; // 导入用户store

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Practice',
    component: () => import('../views/Practice.vue'),
    children: [
      {
        path: 'practice-settings/:module',
        name: 'PracticeSettings',
        component: () => import('../views/Practice/PracticeSettingsPage.vue'),
        props: true
      },
      {
        path: 'paper-test/settings',
        name: 'PaperTestSettings',
        component: () => import('../views/Practice/PaperTestSettingsPage.vue'),
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/achievements',
    name: 'Achievements',
    component: () => import('../views/User/Achievements.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Auth/Login.vue'),
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Auth/Register.vue'),
  },
  {
    path: '/forgot-password',
    name: 'ForgotPassword',
    component: () => import('../views/Auth/ForgotPassword.vue'),
  },
  // 练习模块路由
  {
    path: '/new-politics-practice',
    name: 'NewPoliticsPractice',
    component: () => import('../views/Practice/Politics.vue')
  },
  {
    path: '/new-common-sense-practice',
    name: 'NewCommonSensePractice',
    component: () => import('../views/Practice/CommonSense.vue')
  },
  {
    path: '/new-language-practice',
    name: 'NewLanguagePractice',
    component: () => import('../views/Practice/Language.vue')
  },
  {
    path: '/new-quantity-practice',
    name: 'NewQuantityPractice',
    component: () => import('../views/Practice/Quantity.vue')
  },
  {
    path: '/new-judgment-practice',
    name: 'NewJudgmentPractice',
    component: () => import('../views/Practice/Judgment.vue')
  },
  {
    path: '/new-data-analysis-practice',
    name: 'NewDataAnalysisPractice',
    component: () => import('../views/Practice/DataAnalysis.vue')
  },
  {
    path: '/new-vocabulary-accumulation-practice',
    name: 'NewVocabularyAccumulationPractice',
    component: () => import('../views/Practice/Vocabulary.vue')
  },
  // 练习设置页面路由已移至Practice.vue的子路由中
  // 保留下面的旧路由用于兼容，后续可以移除
  {
    path: '/practice-settings/:module',
    redirect: (to) => {
      return {
        name: 'PracticeSettings',
        params: { module: to.params.module }
      }
    }
  },
  {
    path: '/daily-accumulation-practice',
    name: 'DailyAccumulationPractice',
    component: () => import('../views/Practice/DailyAccumulation.vue')
  },
  {
    path: '/wrong-questions',
    name: 'WrongQuestions',
    component: () => import('../views/Practice/WrongQuestions.vue'),
  },
  {
    path: '/study-system',
    component: () => import('../views/System/StudySystem.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'StudySystem',
        redirect: '/study-system/profile'
      },
      {
        path: 'dashboard',
        name: 'DashboardAlias',
        component: () => import('../views/User/Statistics.vue'),
        meta: { requiresAuth: true, title: '首页' },
      },
      {
        path: 'user-management',
        name: 'UserManagement',
        component: () => import('../views/Management/UserManagement.vue'),
        meta: { requiresAuth: true, title: '用户管理' },
      },
      {
        path: 'error-analysis',
        name: 'ErrorAnalysis',
        component: () => import('../views/QuestionBank/WrongQuestions.vue'),
        meta: { requiresAuth: true, title: '错题本' },
      },
      {
        path: 'study-statistics',
        name: 'StudyStatistics',
        component: () => import('../views/User/Statistics.vue'),
        meta: { requiresAuth: true, title: '学习统计' },
      },
      {
        path: 'profile',
        name: 'UserProfile',
        component: () => import('../views/User/Profile.vue'),
        meta: { requiresAuth: true, title: '个人中心' },
      },
      {
        path: 'question-bank/politics',
        name: 'PoliticsQuestionBank',
        component: () => import('../views/QuestionBank/Politics.vue'),
        meta: { requiresAuth: true, title: '时政题库' },
      },
      {
        path: 'question-bank/common-sense',
        name: 'CommonSenseQuestionBank',
        component: () => import('../views/QuestionBank/CommonSense.vue'),
        meta: { requiresAuth: true, title: '常识题库' },
      },
      {
        path: 'question-bank/language',
        name: 'LanguageQuestionBank',
        component: () => import('../views/QuestionBank/Language.vue'),
        meta: { requiresAuth: true, title: '言语题库' },
      },
      {
        path: 'question-bank/quantity',
        name: 'QuantityQuestionBank',
        component: () => import('../views/QuestionBank/Quantity.vue'),
        meta: { requiresAuth: true, title: '数量题库' },
      },
      {
        path: 'question-bank/judgment',
        name: 'JudgmentQuestionBank',
        component: () => import('../views/QuestionBank/Judgment.vue'),
        meta: { requiresAuth: true, title: '判断题库' },
      },
      {
        path: 'question-bank/data-analysis',
        name: 'DataAnalysisQuestionBank',
        component: () => import('../views/QuestionBank/DataAnalysis.vue'),
        meta: { requiresAuth: true, title: '资料题库' },
      },
      {
        path: 'question-bank/shenlun',
        name: 'ShenlunQuestionBank',
        component: () => import('../views/QuestionBank/Shenlun.vue'),
        meta: { requiresAuth: true, title: '申论题库' },
      },

      {
        path: 'note',
        name: 'NoteQuestionBank',
        component: () => import('../views/QuestionBank/Note.vue'),
        meta: { requiresAuth: true, title: '我的笔记' },
      },
      {
        path: 'daily-accumulation/common-sense',
        name: 'DailyAccumulationCommonSense',
        component: () => import('../views/Accumulation/CommonSense.vue'),
        meta: { requiresAuth: true, title: '常识积累' },
      },
      {
        path: 'daily-accumulation/politics',
        name: 'DailyAccumulationPolitics',
        component: () => import('../views/Accumulation/Politics.vue'),
        meta: { requiresAuth: true, title: '时政积累' },
      },
      {
        path: 'daily-accumulation/writing-material',
        name: 'DailyAccumulationWritingMaterial',
        component: () => import('../views/Accumulation/WritingMaterial.vue'),
        meta: { requiresAuth: true, title: '作文素材' },
      },
      {
        path: 'daily-accumulation/vocabulary',
        name: 'DailyAccumulationVocabulary',
        component: () => import('../views/Accumulation/Vocabulary.vue'),
        meta: { requiresAuth: true, title: '词语积累' },
      },
      {
        path: 'ai-chat-assistant',
        name: 'AIChatAssistant',
        component: () => import('../views/Tools/AIAssistant.vue'),
        meta: { requiresAuth: true, title: 'AI聊天助手' },
      },
      {
        path: 'note',
        name: 'Note',
        component: () => import('../views/QuestionBank/Note.vue'),
        meta: { requiresAuth: true, title: '我的笔记' },
      },
      {
        path: 'oss-image-manager',
        name: 'OssImageManager',
        component: () => import('../views/Tools/ImageManager.vue'),
        meta: { requiresAuth: true, title: 'OSS图片管理' },
      },
      // 管理功能路由
      {
        path: 'achievement-management',
        name: 'AchievementManagement',
        component: () => import('../views/Management/AchievementManagement.vue'),
        meta: { requiresAuth: true, title: '成就管理' },
      },
      {
        path: 'manual-import',
        name: 'ManualImport',
        component: () => import('../views/Management/QuestionImport.vue'),
        meta: { requiresAuth: true, title: '手动导入题目' },
      },
      {
        path: 'paper-management',
        name: 'PaperManagement',
        component: () => import('../views/Management/PaperManagement.vue'),
        meta: { requiresAuth: true, title: '试卷管理' },
      },
      {
        path: 'paper-edit/:id',
        name: 'PaperEdit',
        component: () => import('../views/Management/PaperEdit.vue'),
        meta: { requiresAuth: true, title: '编辑试卷' },
        props: true
      },
      {
        path: 'paper-score-management',
        name: 'PaperScoreManagement',
        component: () => import('../views/Management/PaperScoreManagement.vue'),
        meta: { requiresAuth: true, title: '试卷分值管理' },
      },
      {
        path: 'question-bank-management',
        name: 'QuestionBankManagement',
        component: () => import('../views/Management/QuestionBankManagement.vue'),
        meta: { requiresAuth: true, title: '题库管理' },
      },
      {
        path: 'vocabulary-management',
        name: 'VocabularyManagement',
        component: () => import('../views/Management/VocabularyManagement.vue'),
        meta: { requiresAuth: true, title: '词汇管理' },
      }
    ],
  },
  // AI聊天助手独立路由
  {
    path: '/ai-assistant',
    name: 'AIAssistant',
    component: () => import('../views/Tools/AIAssistant.vue'),
    meta: { requiresAuth: true }
  },
  // 笔记详情页
  {
    path: '/note-detail',
    name: 'NoteDetail',
    component: () => import('../views/QuestionBank/NoteDetail.vue'),
    meta: { requiresAuth: true }
  },
  // 常识积累详情页
  {
    path: '/common-sense-detail',
    name: 'CommonSenseDetail',
    component: () => import('../views/Accumulation/CommonSenseDetail.vue'),
    meta: { requiresAuth: true }
  },
  // 时政积累详情页
  {
    path: '/politics-detail',
    name: 'PoliticsDetail',
    component: () => import('../views/Accumulation/PoliticsDetail.vue'),
    meta: { requiresAuth: true }
  },
  // 作文素材详情页
  {
    path: '/essay-detail',
    name: 'EssayDetail',
    component: () => import('../views/Accumulation/EssayDetail.vue'),
    meta: { requiresAuth: true }
  },
  // 试卷测验路由
  {
    path: '/paper-test',
    name: 'PaperTest',
    component: () => import('../views/Practice/PaperTest.vue'),
    meta: { requiresAuth: true }
  },
  // 重定向到子路由
  {
    path: '/paper-test/settings',
    redirect: () => {
      return { name: 'PaperTestSettings' }
    }
  },
  {
    path: '/paper-test/practice',
    name: 'PaperTestPractice',
    component: () => import('../views/Practice/PaperTestPractice.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/paper-test/result',
    name: 'PaperTestResult',
    component: () => import('../views/Practice/PaperTestResult.vue'),
    meta: { requiresAuth: true }
  }

];

export const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  // 使用Pinia store检查登录状态
  const userStore = useUserStore();
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  
  if (requiresAuth && !userStore.isLoggedIn) {
    // 如果需要认证但未登录，重定向到登录页
    next('/login');
  } else if (to.path === '/login' && userStore.isLoggedIn) {
    // 如果已登录但访问登录页，重定向到后台管理系统
    next('/study-system');
  } else {
    // 其他情况正常导航
    next();
  }
});